package altSpeedometer;

import java.util.*;

public class Speedometer {
    private int currentSpeed;
    private List<SpeedometerListener> listeners;
 
    public Speedometer() {
        currentSpeed= 0;
        listeners = new ArrayList<SpeedometerListener>();
    }
 
    public void setCurrentSpeed(int speed) {
        currentSpeed = speed;
 
        // Tell all observers so they know speed has changed...
        fireSpeedChanged();
    }
 
    public int getCurrentSpeed() {
        return currentSpeed;
    }
 
    public void addSpeedometerListener(SpeedometerListener obj) {
        listeners.add(obj);
    }
 
    public void removeSpeedometerListener(SpeedometerListener obj) {
        listeners.remove(obj);
    }
 
    protected void fireSpeedChanged() {
        SpeedometerEvent speedEvent = new SpeedometerEvent(this, getCurrentSpeed());
 
        for (SpeedometerListener eachListener : listeners) {
            eachListener.speedChanged(speedEvent);
        }
    }
}